package com.example.bluetoothchat.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.bluetoothchat.R;
import com.example.bluetoothchat.base.BaseActivity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class BlueToothListActivity extends BaseActivity {

    private ListView lvList;
    private List<String> info = new ArrayList<>();
//    private ListAdapter adapter;
    private ArrayAdapter<String> adapter;
    private BluetoothAdapter bluetoothAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_blue_tooth_list;
    }

    @Override
    protected void init() {
        lvList = (ListView) findViewById(R.id.lv_list);

        adapter = new ArrayAdapter<String>(BlueToothListActivity.this,R.layout.item);
        lvList.setAdapter(adapter);

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> bluetoothDevices = bluetoothAdapter.getBondedDevices();
        if (bluetoothDevices != null && bluetoothDevices.size() > 0){
            for (BluetoothDevice d :
                    bluetoothDevices) {
                info.add(d.getName() + "\n" + d.getAddress());
                adapter.notifyDataSetChanged();
            }
        }

        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String msg = ((TextView)view).getText().toString();
                String[] s = msg.split("\n");
                Intent intent = new Intent();
                intent.putExtra("adress",s[1]);
                setResult(RESULT_OK,intent);
                finish();
            }
        });

        Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
        for (BluetoothDevice bondedDevice : bondedDevices) {
            adapter.add(bondedDevice.getName()  + "\n" + bondedDevice.getAddress());
        }

//        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//        registerReceiver(broadcastReceiver,intentFilter);
//        IntentFilter intentFilter1 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
//        registerReceiver(broadcastReceiver,intentFilter1);
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (BluetoothDevice.ACTION_FOUND.equals(action)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getBondState() != BluetoothDevice.BOND_BONDED){
                    adapter.add(device.getName() + "\n" + device.getAddress());
                }
            }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                if (adapter.getCount() == 0){
                    showToast("没有蓝牙可连接");
                }else {
                    showToast("点击条目进行连接");
                }
            }
        }
    };

    private void searchNear(){
        if (bluetoothAdapter.isDiscovering()) bluetoothAdapter.cancelDiscovery();
        info.clear();
        bluetoothAdapter.startDiscovery();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_list_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.search:
                showToast("开始搜索...");
                searchNear();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    class ListAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return info.size();
        }

        @Override
        public Object getItem(int i) {
            return info.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(BlueToothListActivity.this).inflate(R.layout.search_blue_tooth_item,viewGroup,false);
            TextView t1 = findViewById(R.id.tv_t1);
            TextView t2 = findViewById(R.id.tv_t2);
            t1.setText(info.get(i));
            return view;
        }
    }
}